package com.crawleService.shanxitobaccoWebsite.service.impl;

import com.crawleService.shanxitobaccoWebsite.bean.CustomObject;
import com.crawleService.shanxitobaccoWebsite.service.LoginShanxiTobaccoCrawlerService;
import com.google.gson.Gson;
import com.projectCommon.bean.Info;
import com.projectCommon.util.commonUtil.CUtil;
import okhttp3.*;
import org.junit.Test;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;


@Service
public class LoginShanxiTobaccoCrawlerImpl implements LoginShanxiTobaccoCrawlerService {
    // TODO:   就这么一个方法是能用的，其他都不能用，因为会重定向跳转
    @Override
    public byte[] getShanXiTobaccoLoginVerificationCodeImage() {
        return new byte[0];
    }

    private String JSESSIONID;
    private String wppm;
    private String jwt;
    private String wdk_user;


    /**
     * 得到验证码图片，返回一个验证码图片的byte数组
     *
     * @return
     */


    public void getVerificationCode() {

        OkHttpClient client = new OkHttpClient();
        String url = "https://sx.jinye.cn/wdk?action=ecw.page&method=verifycode&w=135&h=43&uuid=" + Info.uuid;
        Request request = new Request.Builder()
                .url(url)
                .build();
        try {
            Response response = client.newCall(request).execute();
            ByteArrayInputStream bis = new ByteArrayInputStream(response.body().bytes());
            BufferedImage image = ImageIO.read(bis);
            // 保存图片到本地文件
            File output = new File("output2.jpg");
            ImageIO.write(image, "jpg", output);
            bis.close();
            System.out.println("验证码保存成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    得到JSessionId
    @Test
    public void createJSESSIONID() throws IOException {
//        https://shanxitobacco.com/wdk?action=ecw.page&method=display&site_id=web&inclient=&page_id=page_login
        OkHttpClient client = new OkHttpClient();
        String url = "https://sx.jinye.cn/wdk?action=ecw.page&method=display&site_id=web&inclient=&page_id=page_login"; // 替换为目标网址
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        CUtil.set_SetCookie(response.headers(),Info.ShanxiTobaccoCookieMap);

    }

    //TODO  总结出来就是自己的编码方式不对！！！ 导致了请求到的数据不合理   json那部分！！可能是某个字段写错了，  使用原班数据 单独进行一个加密 看看  和浏览器加密的结果一样不
    public void verifyLogin() {
        OkHttpClient client = new OkHttpClient();
        Cookie cookie = new Cookie.Builder()
                .name("JSESSIONID")
                .value(JSESSIONID)
                .domain("shanxitobacco.com")
                .path("/")
                .httpOnly()
                .build();
        String url = "https://sx.jinye.cn/wdk?action=ecw.page&method=login&ajaxparamtime=" + System.currentTimeMillis(); // 替换为目标网址
        // 构建表单数据
        FormBody formBody = new FormBody.Builder()
                .add("wppm", wppm)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .header("Cookie", cookie.toString())
                .post(formBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String json = response.body().string();
            Gson gson = new Gson();
            CustomObject customObject = gson.fromJson(json,CustomObject.class);
            System.out.println(customObject);
            CUtil.set_SetCookie(response.headers(),Info.ShanxiTobaccoCookieMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getPassword() throws IOException {
        createJSESSIONID();
        getVerificationCode();
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        createWppm(s);
        // 构建最终的参数字符串
        verifyLogin();
    }

    public void createWppm(String code) {
//        data data = new data(code, Info.uuid);
//        String encipher = SEncipherUtil.encipher(data);
        wppm="encipher";
//    }
    }
}
